package cn.itsource.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.OrderAdoptDto;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.org.pojo.info.LoginInfo;
import cn.itsource.pay.service.AlipayService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private AlipayService alipayService;


    /**
     * @param dto petId addressId
     * ctrl + G :找到这个方法被哪些地方调用了
     */
    @Override
    @Transactional
    public AjaxResult submitOrder(OrderAdoptDto dto) {
        //1.通过地址id查询用户地址对象
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(dto.getAddressId());
        //2.copy数据到订单地址
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);
        //3.入库订单地址
        orderAddressMapper.insertSelective(orderAddress);

        //4.通过宠物id获取宠物对象
        Pet pet = petMapper.selectByPrimaryKey(dto.getPetId());

        //5.封装订单对象 (把订单地址的id设置到订单表的address_id外键)
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("订单入库,"+ JSONObject.toJSONString(dto));
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getSaleprice()));
        //雪花id
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        orderAdopt.setOrderSn(orderNo);
        orderAdopt.setPetId(pet.getId());
        orderAdopt.setUserId(LoginInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setAddressId(orderAddress.getId());

        //6.新增订单数据入库
        orderAdoptMapper.insertSelective(orderAdopt);

        /*返回一个<form表单>*/
        return AjaxResult.createSuccess(alipayService.createPayUrl(orderNo,pet.getSaleprice(), "宋睿睿"));
        //7.返回AjaxResult
        //return AjaxResult.createSuccess();
    }

    public static void main(String[] args) {
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
        System.out.println(IdUtil.getSnowflake().nextIdStr());
    }
}
